package cms.manage.template.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cms.bean.question.QuestionTag;
import cms.bean.template.Forum;
import cms.service.question.QuestionTagService;
import cms.manage.QuestionTagManage;

/**
 * 问题标签 -- 模板方法实现
 *
 */
@Component
public class QuestionTagTemplateManage  extends BaseTemplateManage{
	@Resource QuestionTagService questionTagService; 
	@Resource QuestionTagManage questionTagManage;
	
	/**
	 * 标签列表 -- 集合
	 * @param forum
	 */
	public List<QuestionTag> questionTagCollection(Forum forum, Map<String,Object> parameter, Map<String,Object> runtimeParameter){
		List<QuestionTag> questionTagList =  questionTagService.findAllQuestionTagCache();
		List<QuestionTag> questionRootTagList = new ArrayList<QuestionTag>();//排序后标签
		
		if(questionTagList != null && questionTagList.size() >0){
			//组成排序数据
			Iterator<QuestionTag> iterator = questionTagList.iterator();
			while(iterator.hasNext()){
				QuestionTag questionTag = iterator.next();
				//如果是根节点
				if(questionTag.getParentId().equals(0L)){
					questionRootTagList.add(questionTag);
					iterator.remove();
			    }  
			}
			//组合子标签
			for(QuestionTag questionTag :questionRootTagList){
				questionTagManage.childQuestionTag(questionTag,questionTagList);
			}
			//排序
			questionTagManage.questionTagSort(questionRootTagList);

		}
		return questionRootTagList;
	}
}
